package cn.biplam.back.operator.systemManage.caption;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Database;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.file.ObjectFile;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.tools.SYSTEM;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 系统界面标题文字数据访问类(持久化)
 * @author XiaoRuxing
 * @version 1.0
 */
public class SysLanguage extends DataOperator{

	/** 数据库查询实例 */
	private Database database;
	/** 语言类型 */
	private KvMap<String, String> language;
	/** 标题 */
	private static KvMap<String,Caption> captions=null;

	/** 标题文件路径 */
	private String sFilePath;

	public SysLanguage() throws ErrorMessage{
		super("sys_language");
		_default();
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		switch(TOOLS.stringToInt(this.opId)){
		case 28910201://获取标题名称
			String captionSrc=TOOLS.getValueByPropertyName(SysName.PropertiesPath, "Caption.src");
			if(!(captionSrc!=null && !captionSrc.equalsIgnoreCase(""))){
				throw new ErrorMessage("Get param Caption from config file failed!");
			}
			this.result.setStrResult(this.get(filter,TOOLS.stringToInt(captionSrc)));
			return this.result;
		default:
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}

	/**
	 * 初始化语言类型数据(暂时手动写，可以从数据库读取)
	 * @throws ErrorMessage
	 */
	private void _default() throws ErrorMessage{
		language=new KvMap<String, String>();
		language.put("1","chinese");
		language.put("2","english");
	}

	/**
	 * 查询标题内容
	 * @param mUser
	 * @param captionID
	 * @return
	 * @throws ErrorMessage
	 */
	private String get(DataFilter filter,int src) throws ErrorMessage{
		//初始化标题容器
		if(captions==null){
			captions=new KvMap<String, Caption>();
			switch(src){
			case 1:
				this._initFromDb();
				break;
			case 2:
				this._initFromFile();
				break;
			default:
				throw new ErrorMessage("Caption src param is invalid:"+ src+" !");
			}
		}
		//查询标题
		String curCaption=""; 
		Caption caption=captions.getByKey(filter.getValue("user_language"));
		if(caption != null){
			curCaption=caption.get(filter.getValue("caption_id"));
		}
		return curCaption;
	}
	
	/**
	 * 初始化标题容器(从数据库)
	 * @throws ErrorMessage
	 */
	private void _initFromDb() throws ErrorMessage{
		Iterator<Map.Entry<String,String>> lan=language.getIterator();
		Map.Entry<String,String> ent;
		while(lan.hasNext()){
			ent=lan.next();
			/** 从数据库读取数据 */
			Caption caption=readCaptionFromDb(ent.getKey());
			if(ent.getKey()==null || ent.getKey().equalsIgnoreCase("")){
				throw new ErrorMessage("Language code is not found!");
			}
			if(caption==null){
				throw new ErrorMessage("Caption read failed!");
			}
			captions.put(ent.getKey(),caption);
		}
	}

	/**
	 * 初始化标题容器(从文件)
	 * @throws ErrorMessage
	 */
	private void _initFromFile() throws ErrorMessage{
		sFilePath=new File(SYSTEM.getClassPath()).getParentFile().getParent()+"\\data\\caption\\";
		Iterator<Map.Entry<String,String>> lan=language.getIterator();
		while(lan.hasNext()){
			getCaptionFromFile(lan.next().getKey());
		}
	}

	/**
	 * 从文件获取标题
	 * @param sFileName
	 * @throws ErrorMessage
	 */
	private void getCaptionFromFile(String languageCode) throws ErrorMessage{
		String sFileName=sFilePath+language.getByKey(languageCode);
		Caption caption=(Caption)new ObjectFile(sFileName).get();
		captions.put(languageCode,caption);
	}


	/**
	 * 从数据库读取标题 
	 * @param languageCode
	 * @return
	 * @throws ErrorMessage 
	 */
	private Caption readCaptionFromDb(String languageCode) throws ErrorMessage{
		this.database=new Database(new DbResource(DbResource.RT),null,null);
		SqlResult sr=null;
		StringBuilder sp=new StringBuilder();
		sp.append(" select l_id, l_type, l_desc, l_code ");
		sp.append(" from sys_language ");
		sp.append(" where l_type="+languageCode);
		sr=database.executeSelect(new Sql(sp.toString()));
		if(sr==null){
			throw new ErrorMessage("Read caption from db is null!");
		}
		String captionCode=null;
		String captionDesc=null;
		Caption caption=new Caption();
		for(int i=0;i<sr.size();i++){
			captionCode=sr.get(i,"l_code");
			captionDesc=sr.get(i,"l_desc");
			caption.put(captionCode, captionDesc);
		}
		return caption;
	}

	/**
	 * 读取数据，序列化到文件
	 * @throws ErrorMessage
	 */
	public void serialize() throws ErrorMessage{
		
		Iterator<Map.Entry<String,String>> lan=language.getIterator();
		Caption caption=null;
		while(lan.hasNext()){
			Map.Entry<String,String> ent=lan.next();
			/** 从数据库读取数据 */
			caption=readCaptionFromDb(ent.getKey());
			/** 序列化到文件 */
			writeCaptionToFile(ent.getValue(),caption);
		}
	}

	/**
	 * 写标题对象到文件
	 * @throws ErrorMessage
	 */
	private void writeCaptionToFile(String languageName,Caption caption) throws ErrorMessage{
		String sFileName=sFilePath+languageName;	
		ObjectFile of=new ObjectFile(caption,sFileName);
		of.put();
	}
}
